//
//  MemberInfoTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/20.
//

import UIKit

class MemberInfoTableViewCell: UITableViewCell {
    var titleLab: UILabel!
    var arrowsBtn: UIButton!
    var idLab: UILabel!
    
    var bkgView: UIView! //颜色背景
    var bgImageView: UIImageView! //图片
    var bkgLayer: CAGradientLayer! //渐变

    var vipLab: UILabel!
    var memberRewardsBtn: UIButton!
    var integralBtn: UIButton!
    var integralNumBtn: UIButton!

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        
        self.titleLab = UILabel(frame: .zero)
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.font = UIFont(name: Theme.PingFangSCMedium, size: 18)
        self.contentView.addSubview(self.titleLab)
        
        self.arrowsBtn = UIButton(frame: .zero)
        self.arrowsBtn.setImage(UIImage(named: "icon_news_arrows"), for: .normal)
        self.arrowsBtn.setImage(UIImage(named: "icon_news_arrows"), for: .highlighted)
        self.arrowsBtn.addTarget(self, action: #selector(arrowsBtnAntion), for: .touchUpInside)
        self.contentView.addSubview(self.arrowsBtn)
        
        self.idLab = UILabel(frame: .zero)
        self.idLab.textColor = Theme.titleLabColor
        self.idLab.font = UIFont(name: Theme.PingFangSCRegular, size: 14)
        self.contentView.addSubview(self.idLab)
        
        self.bkgView = UIView()
        self.bkgView.backgroundColor = Theme.eColor
        self.bkgView.clipsToBounds = true
        self.contentView.addSubview(self.bkgView)
        
        self.bgImageView = UIImageView(frame: .zero)
        self.bgImageView.isUserInteractionEnabled = true
        self.bkgView.addSubview(self.bgImageView)
        
        
        self.vipLab = UILabel(frame: .zero)
        self.vipLab.textColor = UIColor(hex: 0xE2CA8A)
        self.vipLab.font = UIFont(name: Theme.PingFangSCMedium, size: 16)
        self.bkgView.addSubview(self.vipLab)
        
        
        self.memberRewardsBtn = UIButton(frame: .zero)
        self.memberRewardsBtn.setImage(UIImage(named: "icon_arrow_right_black"), for: .normal)
        self.memberRewardsBtn.setImage(UIImage(named: "icon_arrow_right_black"), for: .highlighted)
        self.memberRewardsBtn.addTarget(self, action: #selector(memberRewardsBtnAntion), for: .touchUpInside)
        self.memberRewardsBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 11)
        self.memberRewardsBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.memberRewardsBtn.semanticContentAttribute = .forceRightToLeft
        self.memberRewardsBtn.layer.cornerRadius = 9
        self.memberRewardsBtn.layer.masksToBounds = true
        self.memberRewardsBtn.backgroundColor = UIColor(hex: 0xE2CA8A)
        self.memberRewardsBtn.setTitle(MMLocalizedString("member_rewards", comment: "会员奖赏"), for: .normal)
//        self.memberRewardsBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        self.bkgView.addSubview(self.memberRewardsBtn)
        
        
        self.integralBtn = UIButton(frame: .zero)
        self.integralBtn.setImage(UIImage(named: "icon_signin"), for: .normal)
        self.integralBtn.setImage(UIImage(named: "icon_signin"), for: .highlighted)
        self.integralBtn.isUserInteractionEnabled = false
        self.integralBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 13)
        self.integralBtn.setTitle(MMLocalizedString("integral", comment: "积分"), for: .normal)
        self.integralBtn.setTitleColor(UIColor(hex: 0xE2CA8A), for: .normal)
        self.integralBtn.contentHorizontalAlignment = .left
        self.bkgView.addSubview(self.integralBtn)
        
        
        self.integralNumBtn = UIButton(frame: .zero)
        self.integralNumBtn.setImage(UIImage(named: "icon_yellow_arrows"), for: .normal)
        self.integralNumBtn.setImage(UIImage(named: "icon_yellow_arrows"), for: .highlighted)
        self.integralNumBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 13)
        self.integralNumBtn.setTitleColor(UIColor(hex: 0xE2CA8A), for: .normal)
        self.integralNumBtn.semanticContentAttribute = .forceRightToLeft
        self.integralNumBtn.contentHorizontalAlignment = .right
        self.integralNumBtn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 4)
        self.integralNumBtn.addTarget(self, action: #selector(integralNumBtnAntion), for: .touchUpInside)
        self.bkgView.addSubview(self.integralNumBtn)
        

//        self.titleLab.text = "YYIIOAOPS"
//        self.idLab.text = "ID：18012345678"
//        self.vipLab.text = "VIP会员"
//        self.integralNumBtn.setTitle("12,800", for: .normal)
        self.addLayout()
        
    }
    
    func addLayout(){
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(14)
            make.height.equalTo(25)
        }
        
        self.arrowsBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(self.titleLab.snp.right).offset(6)
            make.width.height.equalTo(25)
        }
        
        self.idLab.snp.makeConstraints { make in
            make.top.equalTo(self.titleLab.snp.bottom)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        bkgView.snp.makeConstraints { make in
            make.top.equalTo(self.idLab.snp.bottom).offset(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(100)
            make.bottom.equalTo(0)
        }
        
        self.bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.vipLab.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.left.equalTo(20)
            make.height.equalTo(22)
        }
        
        self.memberRewardsBtn.titleLabel?.sizeToFit()
        let temp_width = memberRewardsBtn.titleLabel?.frame.size.width ?? 80
        self.memberRewardsBtn.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.right.equalTo(-20)
            make.width.greaterThanOrEqualTo(temp_width + 20)
            make.height.equalTo(18)
        }
        
        self.integralBtn.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.height.equalTo(20)
            make.width.greaterThanOrEqualTo(60)
            make.bottom.equalTo(self.bkgView.snp.bottom).offset(-14)
        }
       
        self.integralNumBtn.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.height.equalTo(20)
            make.width.greaterThanOrEqualTo(60)
            make.bottom.equalTo(self.bkgView.snp.bottom).offset(-14)
        }
    }
    
    func setBannerLayer(isVip: Bool) {
        if self.bgImageView != nil {
            self.bgImageView.removeFromSuperview()
            self.bgImageView = nil
        }
        if self.bkgLayer != nil {
            self.bkgLayer.removeFromSuperlayer()
            self.bkgLayer = nil
        }
        
//        var vipLab: UILabel!
//        var memberRewardsBtn: UIButton!
//        var integralBtn: UIButton!
//        var integralNumBtn: UIButton!

        if isVip {
            bgImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: bkgView.frame.size.width, height: bkgView.frame.size.height))
            bgImageView.image = UIImage.init(named: "reward_banner_black")
            bkgView.insertSubview(bgImageView, at: 0)
            self.vipLab.textColor = UIColor.init(hex: 0xE2CA8A)
            self.integralBtn.setup_titleColor(color: UIColor.init(hex: 0xE2CA8A))
            self.integralNumBtn.setup_titleColor(color: UIColor.init(hex: 0xE2CA8A))
        }else{
            self.bkgView.backgroundColor = .white
            self.integralBtn.setup_titleColor(color: UIColor.init(hex: 0x825B1D))
            self.integralNumBtn.setup_titleColor(color: UIColor.init(hex: 0x825B1D))
            self.vipLab.textColor = UIColor.init(hex: 0x825B1D)

            let bound = CGRect(x: 0, y: 0, width: bkgView.frame.size.width, height: bkgView.frame.size.height)
            let bgLayer1 = CAGradientLayer()
            bgLayer1.colors = [UIColor(red: 0.98, green: 0.87, blue: 0.67, alpha: 1).cgColor, UIColor(red: 0.95, green: 0.78, blue: 0.53, alpha: 1).cgColor]
            bgLayer1.locations = [0, 1]
            bgLayer1.frame = bound
            bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
            bgLayer1.endPoint = CGPoint(x: 1, y: 1)
            self.bkgLayer = bgLayer1
            bkgView.layer.insertSublayer(self.bkgLayer, at: 0)
        }
    }
    
    //setModel
    func setModel(model: UserInfoDataModel? = nil) {
        
        self.setBannerLayer(isVip: (model?.levelValue ?? 0) > 0)
        
        let firstName = model?.firstName ?? "" //名字
        let lastName = model?.lastName ?? "" //姓氏
        
        if firstName != "" || lastName != "" {
            self.titleLab.text = String(format: "%@ %@",firstName, lastName)
        }else{
            self.titleLab.text = String(format: "%@", UserModule.shared.loginAccount ?? "")
        }
        
        self.idLab.text = "ID：" + (model?.account ?? "")
        self.vipLab.text = model?.memberLevelVO?.levelName ?? ""
        self.integralNumBtn.setTitle(String(format: "%.0f", model?.integralValue ?? 0), for: .normal)
        
        self.integralBtn.titleLabel?.sizeToFit()
        self.integralNumBtn.titleLabel?.sizeToFit()
        let vip_width = self.integralBtn.titleLabel?.frame.size.width ?? 50
        let integral_width = self.integralNumBtn.titleLabel?.frame.size.width ?? 20
        self.integralBtn.snp.updateConstraints { make in
            make.width.greaterThanOrEqualTo(vip_width + 20)
        }
        self.integralNumBtn.snp.updateConstraints { make in
            make.width.greaterThanOrEqualTo(integral_width + 20)
        }
        self.updateConstraints()
    }
    
    
    ///我的资料
    @objc func arrowsBtnAntion(){
        guard UserModule.shared.isLogined ?? false else {
            XTManageCenter.shared.jumpToLoginView()
            return
        }
        let vc = MyUserInfoViewController()
        self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func memberRewardsBtnAntion(){
        guard UserModule.shared.isLogined ?? false else {
            XTManageCenter.shared.jumpToLoginView()
            return
        }
        let vc = UserRewardViewController()
        self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    ///积分跳转
    @objc func integralNumBtnAntion(){
        guard UserModule.shared.isLogined ?? false else {
            XTManageCenter.shared.jumpToLoginView()
            return
        }
        let vc = UserRewardViewController()
        vc.selectIndex = 2
        self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
